package ${package.ServiceImpl};

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guodi.common.utils.UuidUtil;
import com.guodi.common.utils.ValidateUtil;
import com.guodi.core.secure.utils.AuthUtil;
import org.apache.commons.lang3.StringUtils;
import java.util.Objects;
import java.util.Date;
import com.guodi.common.exception.BaseRuntimeException;
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import org.springframework.stereotype.Service;
<#if cfg[table.name]["firstLowerMapperName"]??>
import javax.annotation.Resource;
</#if>

/**
* ${table.comment!} 服务实现类
* ${cfg["annotationAuthor"]} ${author}
* ${cfg["annotationDate"]} ${date}
*/
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {

}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {

    <#if cfg[table.name]["firstLowerMapperName"]??>
    @Resource
    private ${table.mapperName} ${cfg[table.name]["firstLowerMapperName"]};
    </#if>

    /**
    * 修改前的准备，包括验证数据和填充数据
    * ${cfg["annotationParam"]} ${cfg[table.name]["firstLowerEntityName"]}
    * ${cfg["annotationAuthor"]} ${author}
    * ${cfg["annotationDate"]} ${date}
    */
    private void prepareBeforeUpdate(${entity} ${cfg[table.name]["firstLowerEntityName"]}){
        if (StringUtils.isBlank(${cfg[table.name]["firstLowerEntityName"]}.getId())){
            throw new BaseRuntimeException("请选中一条记录进行修改。");
        }
        LambdaQueryWrapper<${entity}> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(${entity}::getId, ${cfg[table.name]["firstLowerEntityName"]}.getId());
        if (this.${cfg[table.name]["firstLowerMapperName"]}.selectCount(queryWrapper) <= 0){
            throw new BaseRuntimeException("当前修改的记录不存在，请刷新后再试。");
        }
        ${cfg[table.name]["firstLowerEntityName"]}.setModifier(Objects.nonNull(AuthUtil.getUser())? AuthUtil.getUserId().toString() : null);
        ${cfg[table.name]["firstLowerEntityName"]}.setModifyTime(new Date());
        ${cfg[table.name]["firstLowerEntityName"]}.setCreator(null);
        ${cfg[table.name]["firstLowerEntityName"]}.setCreateTime(null);
        // 验证数据是否符合
        ValidateUtil.validateEntity(true, ${cfg[table.name]["firstLowerEntityName"]});
    }

    /**
    * 新增前的准备，包括验证数据和填充数据
    * ${cfg["annotationParam"]} ${cfg[table.name]["firstLowerEntityName"]}
    * ${cfg["annotationAuthor"]} ${author}
    * ${cfg["annotationDate"]} ${date}
    */
    private void prepareBeforeSave(${entity} ${cfg[table.name]["firstLowerEntityName"]}){
        if (StringUtils.isBlank(${cfg[table.name]["firstLowerEntityName"]}.getId())){
            ${cfg[table.name]["firstLowerEntityName"]}.setId(UuidUtil.get32UUID());
        }
        ${cfg[table.name]["firstLowerEntityName"]}.setCreator(Objects.nonNull(AuthUtil.getUser())? AuthUtil.getUserId().toString() : null);
        ${cfg[table.name]["firstLowerEntityName"]}.setCreateTime(new Date());
        ${cfg[table.name]["firstLowerEntityName"]}.setModifier(null);
        ${cfg[table.name]["firstLowerEntityName"]}.setModifyTime(null);
        // 验证数据是否符合
        ValidateUtil.validateEntity(true, ${cfg[table.name]["firstLowerEntityName"]});
    }
}
</#if>
